limsMethodCertificate.detailPage = {

    getBtnToolbar : function() {
        var _this = this;

        if (_this.param.readonly === '1') {
            return {
                type : 'btnToolbar',
                items : [ {
                    type : 'button',
                    text : 'GIKAM.BUTTON.BACK',
                    icon : 'back',
                    onClick : function() {
                        workspace.window.goBack();
                    }
                }, {
                    type : 'button',
                    text : 'GIKAM.BUTTON.VIEWAUDIT',
                    icon : 'view-audit',
                    onClick : function() {
                        Gikam.viewAudit('T_LIMS_METHOD_CERTIFICATE_USER', _this.param.id);
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                    icon: 'submit', color: 'success',
                    onClick : function() {
                        var selections=Gikam.getComp('lims-method-certificate-detail-list-grid').getData();
                        if (Gikam.isEmpty(selections)) {
                            Gikam.alert('T_LIMS_METHOD_CERTIFICATE.NOT.EXSIT');
                            return;
                        }
                        let bool = selections.filter(item => Gikam.isEmpty(item.certdate)||Gikam.isEmpty(item.expdate));
                        if (bool.length > 0){
                            Gikam.alert('The authorization date was not entered!');//未填写授权日期!
                            return false;
                        }
                        var items =[{id: _this.param.id}]
                        Gikam.create('workflow').submit({
                            data : items,
                            pageObject : limsMethodCertificateUser
                        }).done(function() {
                            workspace.window.goBack();
                            Gikam.getComp('lims-method-certificate-user-edit-list-grid').refresh();
                        });
                    }
                }, ]
            }
        }

        if (_this.param.readonly === '2') {
            return {
                type : 'btnToolbar',
                items : [{
                    type: 'button',
                    text: 'GIKAM.BUTTON.BACK',
                    icon: 'back',
                    onClick: function () {
                        workspace.window.goBack();
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.PASS',
                    icon: 'pass',
                    onClick : function() {
                        var formData ={id: _this.param.id}
                        Gikam.create('workflow').pass({
                            data : [formData],
                            pageObject : limsMethodCertificateUser
                        }).done(function() {
                            workspace.window.goBack();
                            Gikam.getComp('lims-method-certificate-user-audit-list-grid').refresh();
                        });
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                    icon: 'reject',
                    onClick : function() {
                        var formData ={id: _this.param.id}
                        Gikam.create('workflow').reject({
                            data : [formData],
                            pageObject : limsMethodCertificateUser
                        }).done(function() {
                            workspace.window.goBack();
                            Gikam.getComp('lims-method-certificate-user-audit-list-grid').refresh();
                        });
                    }
                }]
            };
        }

        return {
            type : 'btnToolbar',
            items : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.BACK',
                icon : 'back',
                onClick : function() {
                    workspace.window.goBack();
                }
            } ,{
                type : 'button',
                text : 'GIKAM.BUTTON.VIEWAUDIT',
                icon : 'view-audit',
                onClick : function() {
                    Gikam.viewAudit('T_LIMS_METHOD_CERTIFICATE_USER', _this.param.id);
                }
            },]
        }
    },

    getBaseInfoTab : function() {
        var _this = this;
        return {
            type : 'tab',
            panels : [ /*{
                title : 'BASEMODULE.COREDATA.METHODS.TAB.BASE_INFO',
                items : [ {
                    type : 'form',
                    url : _this.getCompUrl('baseInfoForm'),
                    service : 'limsMethodCertificateUserServiceImpl',
                    dbTable : 'T_LIMS_METHOD_CERTIFICATE_USER',
                    id : 'lims-method-certificate-user-detail-base-info-form',
                    fields : limsMethodCertificateUser.getBaseInfoFormFields()
                } ]
            } ,*/
                {
                title : 'BASEMODULE.COREDATA.METHODS.TAB.BASE_INFO',
                items : [ {
                    type : 'grid',
                    id : 'lims-method-certificate-detail-list-grid',
                    url : limsMethodCertificate.baseUrl + '/queries/raw',
                    requestData : {
                        personId_EQ : _this.param.id
                    },
                    page:true,
                    service : 'limsMethodCertificateServiceImpl',
                    onBeforeUpdate: function (row, keys) {
                        var actRow = this.getActivedRow()
                        if (keys.includes('certdate') && row.certdate && row.certdate !== '') {
                            if (actRow.expdate && actRow.expdate !== '') {
                                var certdate = new Date(row.certdate)
                                var expdate = new Date(actRow.expdate)
                                if (certdate > expdate) {
                                    Gikam.alert(Gikam.propI18N('T_LIMS_METHOD.METHODTYPE.CERTDATE.TIP'))
                                    this.setData({
                                        certdate: '',
                                        id: actRow.id,
                                        index: actRow.index
                                    }, false)
                                    this.refreshRowById(actRow.id)
                                    return false
                                }
                            }
                        }
                        if (keys.includes('expdate') && row.expdate && row.expdate !== '') {
                            if (actRow.certdate && actRow.certdate !== '') {
                                var certdate = new Date(actRow.certdate)
                                var expdate = new Date(row.expdate)
                                if (certdate > expdate) {
                                    Gikam.alert(Gikam.propI18N('T_LIMS_METHOD.METHODTYPE.CERTDATE.TIP'))
                                    this.setData({
                                        expdate: '',
                                        id: actRow.id,
                                        index: actRow.index
                                    }, false)
                                    this.refreshRowById(actRow.id)
                                    return false
                                }
                            }
                        }
                    },
                    deleteFormatter : function(row) {
                        return row.ext$.methodcode || row.id;
                    },
                    dbTable : 'T_LIMS_METHOD_CERTIFICATE',
                    columns : limsMethodCertificate.getEditListColumns(),
                    readonly: _this.param.readonly != '1',
                    toolbar : _this.getBtnToolbarParam()
                } ]
            }, {
                title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                items: [{
                    type: 'grid',
                    filter: false,
                    page: false,
                    toolbarHidden: true,
                    url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_METHOD_CERTIFICATE_USER&id=' + _this.param.id,
                    columns: limsMethodCertificate.getFlowListColumns()
                }]
            } ]
        }
    },

    getBtnToolbarParam : function() {
        var _this = this;
        var readonly = _this.param.readonly;
        if(readonly=== '1'){
            return [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    var grid = Gikam.getComp('lims-method-certificate-detail-list-grid');
                    var selections = grid.getData();
                    var methodIds  = '';
                    if(!Gikam.isEmpty(selections)){
                        methodIds =selections.map(function (item) {
                            return item.methodId;
                        }).join(",");
                    }

                    Gikam.create('modal', {
                        title: Gikam.choose.getConfig('multiChooseMethodPage').title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig('multiChooseMethodPage').url + Gikam.param({
                            methodIds: methodIds
                        }),
                        onAfterClose: function (rows) {
                            console.log(rows)
                            if (Gikam.isEmpty(rows)) {
                                return;
                            }
                            // 拼接人员ID和人员name
                            var methodids = '';
                            Gikam.each(rows, function (index, item) {
                                methodids += item.id + ',';

                            });
                            console.log(methodids)
                            Gikam.preInsert({
                                modalTitle : 'BASEMODULE.COREDATA.METHODS.MODAL.ADD',
                                //fields : limsMethodCertificate.getBaseInfoFormFields(),
                                url : limsMethodCertificate.baseUrl,
                                param : {
                                    personId : _this.param.id,
                                    methodids : methodids
                                },
                            }).done(function(id) {
                                Gikam.getComp('lims-method-certificate-detail-list-grid').refresh();
                            });
                        }
                    });

                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('lims-method-certificate-detail-list-grid').deleteRows(limsMethodCertificate.baseUrl);
                }
            }, {
                type : 'button',
                text : 'Date OF AUTHORIZATION',//授权日期
                icon : 'edit',
                onClick : function() {
                    var grid = Gikam.getComp('lims-method-certificate-detail-list-grid');
                    var selections = grid.getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    var model = Gikam.create('modal', {
                        id:'lims-method-certificate-detail-date',
                        title: 'Date OF AUTHORIZATION',
                        height: '200',
                        width: '400',
                    });
                    Gikam.create('layout', {
                        renderTo: model.window.$dom,
                        center: {
                            items: [{
                                type: 'form',
                                id: 'lims-method-certificate-detail-date-form',
                                fields: limsMethodCertificate.getBaseInfoFormFields(),
                                columns: 1,
                                titleWidth: 100
                            }, {
                                type: 'btnToolbar',
                                items: [{
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CONFIRM',
                                    class: 'blue',
                                    icon: 'save',
                                    color: 'white',
                                    onClick: function () {
                                        var form = Gikam.getComp('lims-method-certificate-detail-date-form');
                                        if (form.validate()) {
                                            workspace.window.showMask()
                                            var data = form.getData()
                                            var certdate = new Date(data.certdate)
                                            var expdate = new Date(data.expdate)
                                            if (certdate > expdate) {
                                                Gikam.alert(Gikam.propI18N('T_LIMS_METHOD.METHODTYPE.CERTDATE.TIP'))
                                                Gikam.getLastModal().close();
                                                return ;
                                            }

                                            data.id=selections[0].id,
                                                data.ids=selections.map(s => s.id).join(','),
                                                Gikam.each(selections, function(index, item) {
                                                    item.certdate = data.certdate;
                                                    item.expdate = data.expdate;
                                                });
                                            Gikam.postText(limsMethodCertificate.baseUrl+'/updateDate' ,
                                                Gikam.getJsonWrapper("",[null,selections])).done(function () {
                                                grid.refresh();
                                                Gikam.getLastModal().close();
                                            }).always(function () {
                                                Gikam.getLastModal().close();
                                            });
                                        }

                                    }
                                }, {
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CANCEL',
                                    icon: 'back',
                                    onClick: function () {
                                        Gikam.getLastModal().close();
                                    }
                                }]
                            }]
                        }
                    })
                }
            }]
        } else {
            return []
        }
    },



    urlManager : {
        baseInfoForm : limsMethodCertificate.baseUrl + '/{id}',
    },

    getCompUrl : function(name) {
        if (Gikam.isEmpty(this.param.id)) {
            return '';
        }
        return Gikam.printf(this.urlManager[name], {
            id : this.param.id
        });
    },

    createPage : function() {
        Gikam.create('layout', {
            center : {
                items : [ this.getBtnToolbar(), this.getBaseInfoTab() ]
            },
            renderTo : workspace.window.$dom
        });
        return this;
    },

    init : function(param) {
        this.param = param;
        this.createPage();
    }
}
